// Copyright 2021 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package session defines API for the session storage.
package session

import (
	"context"
	"crypto/rand"
	"encoding/base64"
	"fmt"

	"go.chromium.org/luci/server/encryptedcookies/session/sessionpb"
)

// ID identifies a session.
type ID []byte

// String is used to format the ID for logging.
func (id ID) String() string {
	return base64.RawStdEncoding.EncodeToString(id)
}

// GenerateID generates a new random session ID or panics if there's not enough
// entropy.
func GenerateID() ID {
	id := make([]byte, 20)
	if _, err := rand.Read(id); err != nil {
		panic(fmt.Sprintf("failed to generate session ID: %s", err))
	}
	return id
}

// Store is a persistent transactional-capable storage of user sessions.
//
// Session IDs are assumed to be generated by GenerateID, i.e. be high-entropy
// random blobs.
type Store interface {
	// FetchSession fetches an existing session with the given ID.
	//
	// Returns (nil, nil) if there's no such session. All errors are transient.
	FetchSession(ctx context.Context, id ID) (*sessionpb.Session, error)

	// UpdateSession transactionally updates or creates a session.
	//
	// If fetches the session, calls the callback to mutate it, and stores the
	// result. If it is a new session, the callback receives an empty proto.
	//
	// The callback may be called multiple times in case the transaction is
	// retried. Errors from callbacks are returned as is. All other errors are
	// transient.
	UpdateSession(ctx context.Context, id ID, cb func(*sessionpb.Session) error) error
}
